import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../res/color_res.dart';

class ZLText extends StatelessWidget {
  final String text;
  final int color;
  final double size;
  final int maxLines; //传-1为多行
  final FontWeight fontWeight;
  final TextAlign textAlign;

  const ZLText({
    super.key,
    required this.text,
    this.color = colorBlack,
    this.size = 16,
    this.maxLines = 1,
    this.fontWeight = FontWeight.normal,
    this.textAlign = TextAlign.left,
  });

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      maxLines: maxLines == -1 ? null : maxLines,
      overflow: maxLines == -1 ? null : TextOverflow.ellipsis,
      style: TextStyle(
        fontWeight: fontWeight,
        fontSize: size,
        color: Color(color),
        decoration: TextDecoration.none,
      ),
      textAlign: textAlign,
    );
  }
}

class ZLOnTapText extends StatelessWidget {
  final String text;
  final Function onTap;
  final int backgroundColor;
  final double width;
  final double height;
  final int textColor;
  final double textSize;
  final FontWeight fontWeight;
  final double borderRadius;

  const ZLOnTapText({
    super.key,
    required this.text,
    required this.onTap,
    this.backgroundColor = colorTheme,
    this.width = 0,
    this.height = 0,
    this.textColor = colorWhite,
    this.textSize = 0.0,
    this.fontWeight = FontWeight.normal,
    this.borderRadius = 5.0,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusManager.instance.primaryFocus?.unfocus();
        onTap();
      },
      child: Container(
        alignment: Alignment.center,
        width: width == 0 ? 100.w : width,
        height: height == 0 ? 50.w : height,
        decoration: BoxDecoration(
          color: Color(backgroundColor),
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
        ),
        child: ZLText(
          text: text,
          color: textColor,
          size: textSize == 0 ? 18.sp : textSize,
          fontWeight: fontWeight,
        ),
      ),
    );
  }
}

class ZLTextSpan extends TextSpan {
  ZLTextSpan(
    String content, {
    int color = colorBlack,
    double size = 14,
    FontWeight fontWeight = FontWeight.normal,
    Function? onTap,
  }) : super(
          style: TextStyle(
            color: Color(color),
            fontSize: size,
            fontWeight: fontWeight,
            decoration: TextDecoration.none,
          ),
          text: content,
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              if (onTap != null) {
                onTap();
              }
            },
        );
}
